import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { useRouterStore } from './router'

const storeSetup = () => {
  // routerStore
  const routerStore = useRouterStore()

  /* 定义数据--------------------------------------------------------------------- */
  const userInfo = ref({
    userId: '',
    userName: '',
  })

  // 当前用户权限
  const currentUserRole = ref(3)

  // 退出登录处理
  const loginOutDispose = () => {
    localStorage.clear()
    sessionStorage.clear()
    console.log('loginOut')
    routerStore.tabsList = []
  }

  // 设置 token
  const setToken = (token) => {
    localStorage.setItem('accessToken', token)
  }

  // 删除 token
  const removeToken = () => {
    localStorage.removeItem('accessToken')
  }
  /* 定义函数--------------------------------------------------------------------- */

  // 返回参数
  return {
    userInfo,
    loginOutDispose,
    setToken,
    removeToken,
    currentUserRole,
  }
}

// 导出Store
export const useUserStore = defineStore('user', storeSetup, { persist: true })
